package com.dawn.trademanage.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Controller;

import com.dawn.trademanage.bean.TradeInfoBean;
import com.dawn.trademanage.service.TradeManageService;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.bean.UserPreference;
import com.opensymphony.xwork2.ActionSupport;

@Controller("tradeAction")
@Scope("prototype")
public class TradeAction extends ActionSupport implements SessionAware,
		ServletRequestAware {

	private static final long serialVersionUID = 553819690946094426L;

	private TradeInfoBean tradeInfo;
	private Map<String, Object> session;
	private UserPreference userPreference;
	private List<TradeInfoBean> tradeList;
	private HttpServletRequest request;

	@Autowired
	private TradeManageService tradeManageService;

	public String createTrade() {
		if (request.getParameter("type").equals("first"))
			return "login";
		userPreference = (UserPreference) session.get("user");
		long userId = userPreference.getUserId();
		UserBean userBean = new UserBean();
		userBean.setUserId(userId);
		tradeInfo.setTuUserinfo(userBean);
		tradeInfo.setBeginTime(new Date());
		tradeInfo.setIsOver((byte) 0);
		tradeInfo.setRemainingSum(tradeInfo.getTradePrice());
		return tradeManageService.addTrade(tradeInfo) ? SUCCESS : ERROR;
	}

	public String showTradeList() {
		tradeList = tradeManageService.showTradeList();
		if (null == tradeList)
			return ERROR;
		return SUCCESS;
	}

	public TradeInfoBean getTradeInfo() {
		return tradeInfo;
	}

	public void setTradeInfo(TradeInfoBean tradeInfo) {
		this.tradeInfo = tradeInfo;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<TradeInfoBean> getTradeList() {
		return tradeList;
	}

	public void setTradeList(List<TradeInfoBean> tradeList) {
		this.tradeList = tradeList;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
